package com.lxy.dataservice.service;

import cn.hutool.core.util.StrUtil;
import com.lxy.dataservice.constants.MessageConstants;
import com.lxy.dataservice.dao.StaffDao;
import com.lxy.dataservice.entity.Staff;
import com.nip.db.mongodb.utils.tips.Response;
import com.nip.db.mongodb.utils.tips.ResponseCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * StaffService
 *
 * @author < a href=" ">BBB</ a>
 * @version v1.0.01
 * @date 2019-12-31 10:09
 */
@Service
public class StaffService {
  @Autowired
  StaffDao staffDao;

  /**
   * 查找所有员工
   */
  public List<Staff> findAllStaff(){
    return staffDao.findAll();
  }

  /**
   * 根据id查找员工
   */
  public Staff findStaffById(String id){
    return staffDao.findById(id).orElse(new Staff());
  }

  /**
   * 保存员工
   */
  public Response saveStaff(Staff entity){
    try {
      if (StrUtil.isEmpty(entity.getId())){
        entity.setPassword("123456");
      }
      staffDao.save(entity);
      return new Response(ResponseCode.CODE_200, MessageConstants.SUCCESS);
    }catch (Exception e){
      return new Response(ResponseCode.CODE_202,e.getMessage());
    }
  }

  /**
   * 删除员工
   */
  public Response deleteStaffById(String id){
    try {
      staffDao.deleteById(id);
      return new Response(ResponseCode.CODE_200, MessageConstants.SUCCESS);
    }catch (Exception e){
      return new Response(ResponseCode.CODE_202,e.getMessage());
    }
  }

  /**
   * 登录验证
   */
  public Response login(String cardNumber, String password){
    Staff staff = staffDao.findStaffByCardNumber(cardNumber);
    if (StringUtils.isEmpty(staff)) {
      return new Response(ResponseCode.CODE_500,"员工卡号错误");
    }
    if (!password.equals(staff.getPassword())) {
      return new Response(ResponseCode.CODE_500,"密码错误");
    }
    return new Response(ResponseCode.CODE_200, MessageConstants.SUCCESS, staff);
  }

  public List<Staff> findAllByCardNumberLike(String cardNumber){
    return staffDao.findAllByCardNumberLike(cardNumber);
  }
}
